package com.oocl.ita.winton.Main;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

import com.geralt.im.command.Command;
import com.geralt.im.command.impl.InfoCommand;
import com.geralt.im.command.impl.StartCommand;
import com.geralt.im.command.impl.StopCommand;


public class ImControlPanal {
	public static void main(String[] arg) {
		
		InputStream is;
		try {
			ServerSocket server = new ServerSocket(8088);
			while(true){
				Socket socket = server.accept();
				is = socket.getInputStream();
				OutputStream os = socket.getOutputStream();
				BufferedReader br = new BufferedReader(new InputStreamReader(is));
				String line = null;
				line = br.readLine();
				String commandType = line.substring(line.indexOf("?")+1,line.lastIndexOf(" "));
				String html = serverCommandParse(commandType).excete();
				os.write(html.getBytes());
				os.flush();
				os.close();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	public static Command serverCommandParse(String commandString){
		switch (commandString) {
		case "START":
			return new StartCommand();
		case "STOP":
			return new StopCommand();
		default:
			return new InfoCommand();
		}
	}
	
}
